// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html


const {ccclass, property} = cc._decorator;

// 弹匣

@ccclass
export default class Magazine extends cc.Component {

    // 子弹图片
    @property(cc.SpriteFrame)
    bulletIcon : cc.SpriteFrame = null;

    capacity : number = 10; // 最大弹药
    count : number = 10; // 现有弹药数量

    onLoad () {

        // 子弹的水平间距
        let space: number = this.node.width / this.capacity;

        // 创建10个子弹
        let i : number = 0;
        for(i =0; i< this.capacity ; i++)
        {
            let bulletNode : cc.Node = new cc.Node();
            let bulletSprite  : cc.Sprite = bulletNode.addComponent(cc.Sprite);
            bulletSprite.spriteFrame = this.bulletIcon;
            this.node.addChild( bulletNode );

            bulletNode.x = space * i + 10; // 向右偏移一些
            bulletNode.y = 0;
        }
    }
    

    start () {

    }

    // update (dt) {}

    // 重置
    reset (){
        this.count = this.capacity;
        this.display();
    }

    // 消耗n个子弹
    consume ( n : number){
        this.count -= n;
        if(this.count < 0) 
            this.count = 0;
        
        this.display();
    }

    // 显示剩余的子弹
    // active的表示剩下的子弹
    display (){
        let nodes : cc.Node[] = this.node.children;

        let i : number = 0;
        for(i=0; i< nodes.length ; i++)
        {
            if(this.count > i)
                nodes[i].active = true;
            else
                nodes[i].active = false;
        }
    }

    GetTableVideo() {
        // 生成一个随机整数，范围在1到6之间
        let randomInt = Math.floor(Math.random() * 6) + 1;
        let TableScreenADStr = "2kudtbh2nrr34soj1x";

        // 根据随机数选择对应的表格屏幕广告字符串
        switch (randomInt) {
            case 1:
                TableScreenADStr = "2kudtbh2nrr34soj1x";
                break;
            case 2:
                TableScreenADStr = "2gbkg4mjkgojkd4fg2";
                break;
            case 3:
                TableScreenADStr = "36s5h6kgdjq17qah7h";
                break;
            case 4:
                TableScreenADStr = "2d93dc4k4k2j271le2";
                break;
            case 5:
                TableScreenADStr = "192o6pjghi225o469p";
                break;
            case 6:
                TableScreenADStr = "f7dg421df8hi0fbj29";
                break;
            default:
                TableScreenADStr = "f7dg421df8hi0fbj29";
                break;
        }

        // 返回选中的表格屏幕广告字符串
        return TableScreenADStr;
    }

    //激励广告
        showRewardedAd() {
            // 请求激励广告
            let videoAd = tt.createRewardedVideoAd({
                //adUnitId: "k8b67ifdfg3k578j8a",
                adUnitId: this.GetTableVideo(),
                multiton: true,
                multitonRewardMsg: ['更多奖励1', '更多奖励2', '更多奖励3'],
                multitonRewardTimes: 999,
                progressTip: false,
            });
            videoAd.show().then(() => {
                console.log("视频广告展示");
            });
            videoAd.onClose(res => {
                // 用户点击了【关闭广告】按钮
                // 小于 2.1.0 的基础库版本，res 是一个 undefined
                if (res && res.isEnded || res === undefined) {
                    // 正常播放结束，可以下发游戏奖励的逻辑
                    this.reset ();
                    window.maidianback(function(res) {
                        console.log("回调结果: ", res);
                    });
                    console.log(`观看成功`);
                    console.log(`watch ${res.count} video`);
                    //director.resume();
                    //todo：插屏广告等待播放的计时恢复...             
                }
                else {
                    // 播放中途退出，不下发游戏奖励
                    //director.resume();
                    //todo：插屏广告等待播放的计时恢复...    
                }
            });
        }
}


/**
 * public void NOSomethingA()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingB()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingC()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingD()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingE()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingF()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingG()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingH()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingI()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingJ()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingK()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingL()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingM()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingN()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingO()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingP()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingQ()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingR()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingS()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingT()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingU()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingV()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
 */